package com.yeyaomai.dksns.flowis.quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.query.Criteria;
import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.dao.MongoDBBasicDao;
import com.yeyaomai.dksns.flowis.dao.ReferCategoryDao;
import com.yeyaomai.dksns.flowis.domain.ReferCategory;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.util.CollectionNames;
import com.yeyaomai.dksns.flowis.util.Globals;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * 来路分类维度定时任务类
 * @author xiaqin
 *@date 2012-07-09
 */
public class ReferCategoryExecutor extends MongoDBBasicDao<ReferCategory> {

	@Resource
	private DetailDao detailDao;

	@Resource
	private ReferCategoryDao referCategoryDao;

	final Logger logger = Logger.getLogger("quartz");
	/**
	 * 执行添加操作 新访客浏览次数,是指从某个域名过来的第一次访问的用户访问端口的网站的页面次数
	 * 站内总浏览数是指某一类型过来的访问用户访问端口网站的页面次数
	 * 
	 * @throws ParseException
	 */
	public void saveReferCategory() throws ParseException {
		Date date = new Date();
		Date dates = new Date(date.getTime() - 1 * 3600 * 1000);
		String formDate = VelocityToolDateUtils.formathour.format(dates);
		String toDate = VelocityToolDateUtils.formathour.format(date);
		GroupByResults<FlowisStatDetailDTO> result = this.getVisitTypeByTime(
				formDate, toDate);
		ReferCategory referCategory = new ReferCategory();
		referCategory.setReferDate(Globals.FORMAT_TYPE_A
				.parse(Globals.FORMAT_TYPE_A.format(dates)));
		SimpleDateFormat fmat = new SimpleDateFormat("HH");
		referCategory.setHour(fmat.format(dates) + ":00");
		referCategory.setEngineCount(0);
		referCategory.setDirectCount(0);
		referCategory.setExternalCount(0);
		for (FlowisStatDetailDTO engine : result) {
			if (engine.getReq_source() == 1)
				referCategory.setEngineCount(engine.getVisitCount());
			if (engine.getReq_source() == 2)
				referCategory.setDirectCount(engine.getVisitCount());
			if (engine.getReq_source() == 3)
				referCategory.setExternalCount(engine.getVisitCount());
		}
		logger.info(date+"running saveReferCategory Task add Data 1 times");
		referCategoryDao.addReferCategory(referCategory);
		logger.info("#############################来路分类定时任务执行添加完毕######################");
	}

	/**
	 * 定时任务处理UV
	 */
	private GroupByResults<FlowisStatDetailDTO> getVisitTypeByTime(
			String fromTrendDay, String toTrendDay) {
		String mapFunction = "{ visitCount: 0}";
		String reduceFunction = "function(doc, prev) {prev.visitCount+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lt(toTrendDay);
		GroupByResults<FlowisStatDetailDTO> result = detailDao.group(criteria,
				CollectionNames.REQSOURCE, mapFunction, reduceFunction,
				FlowisStatDetailDTO.class);
		return result;
	}

}